package com.googlecode.grima.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.FormService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.googlecode.grima.base.Message;
import com.googlecode.grima.service.ProcessService;

@Controller
public class ProcessController extends PortalController {

	private static final long serialVersionUID = 1L;
	private ProcessDefinition currentProcessDefinition;

	@Autowired
	private ProcessService processService;
	
	@Autowired
	private RuntimeService runtimeService;

	@Autowired
	private FormService formService;
	
	@Autowired
	private RepositoryService repositoryService;

	@RequestMapping(value = "/secure/processes", method = RequestMethod.GET)
	public void list(ModelMap modelMap) {
		List<ProcessDefinition> processes = processService.getProcessDefinitionList();
		modelMap.addAttribute("processes", processes);
	}

	@RequestMapping(value = "/secure/processDetails", method = RequestMethod.GET)
	public void details(@RequestParam("key") String key, ModelMap modelMap) {
		ProcessDefinition process = processService.findProcessDefinitionByKey(key);
		modelMap.addAttribute("process", process);
	}

	@RequestMapping(value = "/secure/startProcessInstance", method = RequestMethod.GET)
	public String startProcessInstance(@RequestParam("key") String key, RedirectAttributes redirectAttributes) {

		Map<String, Object> variables = new HashMap<String, Object>();
		
		runtimeService.startProcessInstanceByKey(key, variables);
		
		showMessage(null, redirectAttributes, Message.SUCCESS, "Sucesso", "Processo iniciado com sucesso!");
		
		return "redirect:/secure/inbox.html";
	}
	
	public ProcessService getProcessService() {
		return processService;
	}

	public void setProcessService(ProcessService processService) {
		this.processService = processService;
	}

	public FormService getFormService() {
		return formService;
	}

	public void setFormService(FormService formService) {
		this.formService = formService;
	}

	public ProcessDefinition getCurrentProcessDefinition() {
		return currentProcessDefinition;
	}

	public void setCurrentProcessDefinition(ProcessDefinition currentProcessDefinition) {
		this.currentProcessDefinition = currentProcessDefinition;
	}

	public RuntimeService getRuntimeService() {
		return runtimeService;
	}

	public void setRuntimeService(RuntimeService runtimeService) {
		this.runtimeService = runtimeService;
	}

	public RepositoryService getRepositoryService() {
		return repositoryService;
	}

	public void setRepositoryService(RepositoryService repositoryService) {
		this.repositoryService = repositoryService;
	}

}